package com.example.myproject.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.myproject.model.CommentReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface CommentReportMapper extends BaseMapper<CommentReport> {
    
    @Select("SELECT COUNT(*) FROM comment_report WHERE comment_id = #{commentId} AND user_id = #{userId}")
    Integer existsByCommentIdAndUserId(@Param("commentId") Long commentId, @Param("userId") Long userId);
    
    @Update("UPDATE comment_report SET status = #{status}, handle_time = NOW() WHERE report_id = #{reportId}")
    Integer updateStatus(@Param("reportId") Integer reportId, @Param("status") String status);




    @Update("UPDATE comment_report SET status = 'succeed' WHERE comment_id = #{commentId} AND status = 'pending'")
    void markAllReportsSucceedByCommentId(Long commentId);
    // 联表查询举报列表，增加 user_status 字段
    @Select("SELECT r.report_id, r.comment_id, c.content AS comment_content, u.user_id AS reported_user_id, u.user_name AS reported_user_name, u.user_status AS reported_user_status, r.reason, r.create_time " +
            "FROM comment_report r " +
            "JOIN comment c ON r.comment_id = c.comment_id " +
            "JOIN user u ON c.user_id = u.user_id " +
            "WHERE r.status = 'pending'")
    List<Map<String, Object>> selectPendingReports();
}
